package com.taru.order.dao;

import com.taru.order.entity.Answer;
import com.taru.order.entity.User;
import com.taru.order.util.DbUtil;
import com.taru.order.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AnswerDaoImpl {

    UserDaoimpl userDaoimpl=new UserDaoimpl();

    /**
     * 添加答案
     */
    public int addAnswer(Answer answer){
        String sql="insert into `tb_answer`(context,user_id,problem_id,create_time) values(?,?,?,now())";
        int code= DbUtil.update(sql,answer.getContext(),answer.getUserId(),answer.getProblemId());
        return code;
    }
    /**
     * 获取答案列表
     */
    public List<Answer> listAnswer(String problemId){
        String sql="select * from `tb_answer` where problem_id=? order by create_time desc";
        List<Map<String,Object>> list = DbUtil.query(sql,problemId);
        List<Answer> answerList=new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                Answer answer=new Answer();
                answer.setId(StringUtil.valueOf(map.get("id")));
                answer.setContext(StringUtil.valueOf(map.get("context")));
                answer.setCreateTime(StringUtil.valueOf(map.get("create_time")));
                answer.setModTime(StringUtil.valueOf(map.get("mod_time")));
                answer.setUserId(StringUtil.valueOf(map.get("user_id")));
                User user=userDaoimpl.userInfo(StringUtil.valueOf(map.get("user_id")));
                answer.setUser(user);
                answer.setProblemId(StringUtil.valueOf(map.get("problem_id")));
                answerList.add(answer);
            }
        }
        DbUtil.close();
        return answerList;

    }
    /**
     * 根据问题id获取最新一个答案
     */
    public List<Answer> listAnswerOne(String problemId){
        String sql="select * from `tb_answer` where problem_id=? order by create_time desc";
        List<Map<String,Object>> list = DbUtil.query(sql,problemId);
        List<Answer> answerList=new ArrayList<>();
        if(list!=null && list.size()>0){
            Map<String,Object> map=list.get(0);
            Answer answer=new Answer();
            answer.setId(StringUtil.valueOf(map.get("id")));
            answer.setContext(StringUtil.valueOf(map.get("context")));
            answer.setCreateTime(StringUtil.valueOf(map.get("create_time")));
            answer.setModTime(StringUtil.valueOf(map.get("mod_time")));
            answer.setUserId(StringUtil.valueOf(map.get("user_id")));
            answer.setProblemId(StringUtil.valueOf(map.get("problem_id")));
            answerList.add(answer);
        }
        DbUtil.close();
        return answerList;
    }
    /**
     * 修改答案
     */
    public int modAnswer(Answer answer){
        String sql="update `tb_answer` set context=?,mod_time=now() where id=?";
        int code= DbUtil.update(sql,answer.getContext(),answer.getId());
        DbUtil.close();
        return code;
    }
    /**
     * 删除答案
     */
    public int deleteAnswer(List list) throws Exception {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            stringBuffer.append("\'");
            stringBuffer.append(list.get(i));
            stringBuffer.append("\',");
        }
        String sql = "delete from `tb_answer` where id in (" + stringBuffer.substring(0, stringBuffer.length() - 1) + ")";
        int code = 0;
        try {
            code = DbUtil.update(sql);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("DML异常");
        }
        DbUtil.close();
        return code;
    }
}
